package main.java.com.healthy.entity;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 用户实体类
 * 对应功能模块：用户认证与权限管理、个人健康档案管理
 */
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    private String userId;
    private String username;
    private String password;
    private String email;
    private String phone;
    private double height; // 身高（厘米）
    private double weight; // 体重（公斤）
    private String gender; // 性别：男/女
    private int age;
    private double bmi; // 身体质量指数
    private String activityLevel; // 活动水平：久坐、轻度活动、中度活动、重度活动、极重度活动
    private String target; // 目标：减重、维持、增肌
    private double targetWeight; // 目标体重
    private LocalDate birthday;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;

    // 默认构造方法
    public User() {
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
    }

    // 带参数的构造方法
    public User(String userId, String username, String password, double height, double weight,
                String gender, int age, String activityLevel, String target) {
        this();
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.height = height;
        this.weight = weight;
        this.gender = gender;
        this.age = age;
        this.activityLevel = activityLevel;
        this.target = target;
        // 自动计算BMI
        calculateBMI();
    }

    // 计算BMI
    public void calculateBMI() {
        if (height > 0) {
            double heightInMeter = height / 100.0;
            this.bmi = weight / (heightInMeter * heightInMeter);
            this.bmi = Math.round(bmi * 100.0) / 100.0; // 保留两位小数
        }
    }

    // Getter和Setter方法
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
        calculateBMI(); // 身高改变时重新计算BMI
        this.updateTime = LocalDateTime.now();
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
        calculateBMI(); // 体重改变时重新计算BMI
        this.updateTime = LocalDateTime.now();
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
        this.updateTime = LocalDateTime.now();
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
        this.updateTime = LocalDateTime.now();
    }

    public double getBmi() {
        return bmi;
    }

    public void setBmi(double bmi) {
        this.bmi = bmi;
    }

    public String getActivityLevel() {
        return activityLevel;
    }

    public void setActivityLevel(String activityLevel) {
        this.activityLevel = activityLevel;
        this.updateTime = LocalDateTime.now();
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
        this.updateTime = LocalDateTime.now();
    }

    public double getTargetWeight() {
        return targetWeight;
    }

    public void setTargetWeight(double targetWeight) {
        this.targetWeight = targetWeight;
        this.updateTime = LocalDateTime.now();
    }

    public LocalDate getBirthday() {
        return birthday;
    }

    public void setBirthday(LocalDate birthday) {
        this.birthday = birthday;
        this.updateTime = LocalDateTime.now();
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    // 获取BMI分类
    public String getBmiCategory() {
        if (bmi < 18.5) {
            return "偏瘦";
        } else if (bmi < 24) {
            return "正常";
        } else if (bmi < 28) {
            return "超重";
        } else {
            return "肥胖";
        }
    }

    @Override
    public String toString() {
        return "User{" +
                "userId='" + userId + '\'' +
                ", username='" + username + '\'' +
                ", height=" + height +
                ", weight=" + weight +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                ", bmi=" + bmi +
                ", activityLevel='" + activityLevel + '\'' +
                ", target='" + target + '\'' +
                ", targetWeight=" + targetWeight +
                ", createTime=" + createTime +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return userId.equals(user.userId);
    }

    @Override
    public int hashCode() {
        return userId.hashCode();
    }
}